//
//  StaffLogic.m
//  QRMaster
//
//  Created by myApple on 15/12/21.
//  Copyright (c) 2015年 myApple. All rights reserved.
//

#import "StaffLogic.h"
#import "OperationDataBaseModel.h"

@interface StaffLogic()
{
    OperationDataBaseModel * operation;
}

@end

@implementation StaffLogic

#pragma mark -------------插入职员信息
-(void)insertStaffData:(StaffModel *)model withCommuntiyId:(NSString *)communityId
{
    NSMutableDictionary * staffDict = [self modelConvertDict:model];
    [staffDict setValue:communityId forKey:@"communityId"];
     operation = [[OperationDataBaseModel alloc]init];
    [operation insertDataTable:@"member" withDict:staffDict];
}


#pragma mark -------------查询职员信息
-(void)queryStaffData:(NSString *)communityId block:(queryStaffData)staffArray
{
    __block NSMutableArray * staffMutableArray = [[NSMutableArray alloc]init];
    NSString * condition = [NSString stringWithFormat:@"where %@=%@",@"communityId",communityId];
    operation = [[OperationDataBaseModel alloc]init];
    [operation queryDataFromTable:@"member" withCondition:condition block:^(FMResultSet *set) {
        while ([set next]) {
            StaffModel * model = [[StaffModel alloc]init];
            model.userID = [set stringForColumn:@"id"];
            model.userName = [set stringForColumn:@"name"];
            model.sex = [set stringForColumn:@"gender"];
            model.headUrl = [set stringForColumn:@"headUrl"];
            model.mobile = [set stringForColumn:@"mobile"];
            model.StaffRole = [set stringForColumn:@"role"];
            model.email = [set stringForColumn:@"email"];
//            model.headerWord = [set stringForColumn:@"headerWord"];
            model.areaCode = [set stringForColumn:@"areaCode"];
            [staffMutableArray addObject:model];
        }
        if (staffArray) {
            staffArray(staffMutableArray);
        }
    }];
}

#pragma mark --------------删除职员信息
-(void)deleteStaffData:(NSString *)memberIdStr communityId:(NSString *)communityId
{
    NSString * condition = [NSString stringWithFormat:@"where id in %@ and communityId = %@",[NSString stringWithFormat:@"(%@)",memberIdStr],communityId];
    operation = [[OperationDataBaseModel alloc]init];
    [operation deleteDataFromTable:@"member" withCondition:condition];
}


#pragma mark -------------删除整个集群下面的职员便于插入
-(void)deleteStaffByCommunityId:(NSString *)communityId
{
    NSString * condition = [NSString stringWithFormat:@"where communityId = %@",communityId];
    operation = [[OperationDataBaseModel alloc]init];
    [operation deleteDataFromTable:@"member" withCondition:condition];
}


#pragma mark --------------更新成员信息
-(void)updateStaffData:(StaffModel *)model communityId:(NSString *)communityId
{
    NSMutableDictionary * staffDict = [[NSMutableDictionary alloc]init];
    [staffDict setValue:model.userName forKey:@"name"];
    [staffDict setValue:model.sex forKey:@"gender"];
    [staffDict setValue:model.StaffRole forKey:@"role"];
    NSString * condition = [NSString stringWithFormat:@" where id = %@ and communityId = %@", model.userID,communityId];
    operation = [[OperationDataBaseModel alloc]init];
    [operation updateDataFromTable:@"member" withCondition:condition withDict:staffDict];
}

/**
 *  模型转化为字典
 *
 *  @param model 职员模型
 *
 *  @return 可变字典
 */
-(NSMutableDictionary *)modelConvertDict:(StaffModel *)model
{
    NSMutableDictionary * staffDict = [[NSMutableDictionary alloc]init];
    [staffDict setValue:model.userID forKey:@"id"];
    [staffDict setValue:model.mobile forKey:@"mobile"];
    [staffDict setValue:model.email forKey:@"email"];
    [staffDict setValue:model.userName forKey:@"name"];
    [staffDict setValue:model.sex forKey:@"gender"];
    [staffDict setValue:model.StaffRole forKey:@"role"];
    [staffDict setValue:model.headerWord forKey:@"firstChar"];
    [staffDict setValue:model.headUrl forKey:@"headUrl"];
    [staffDict setValue:model.areaCode forKey:@"areaCode"];
    return staffDict;
}

-(NSString *)getStr:(NSString *)str
{
    NSString *returnStr=@"";
    if (str==nil || [@"" isEqualToString:str]==YES || str.length==0 ) {
        returnStr=@"";
    }
    else
    {
        returnStr=str;
    }
    return returnStr;
}

@end
